<template>
    <div class="components_edit_modal">
        <el-dialog width="500px" :title="title" :visible.sync="state" :show-close="true" :close-on-click-modal="false">
            <el-form ref="film" :model="film" :rules="rules" label-width="95px">

                <el-form-item label="用 户 名：" prop="userId">
                    <el-select v-model="film.userId" filterable placeholder="请选择">
                        <el-option
                            v-for="item in options"
                            :key="item.userProfile.id"
                            :label="item.userProfile.nickname"
                            :value="item.userProfile.id">
                        </el-option>
                    </el-select>
                    <!-- <el-input v-model.trim="film.userId"></el-input> -->
                </el-form-item>

                <el-form-item label="底片名称：" prop="name">
                    <el-input v-model.trim="film.name"></el-input>
                </el-form-item>

                <el-form-item label="底片链接：" prop="url">
                    <el-input v-model.trim="film.url" style="display:none;"></el-input>
                    <pl-upload
                        :postData="attachment"
                        :displaymode="'images'"
                        :api="'jstn-file'"
                        :imageOnly="false"
                        :buttonIds='{select:"selectfiles1",upload:"postfiles1",container:"container1"}'
                        @reloadFiles="reloadFiles" ></pl-upload>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" size="small" @click="addfilm">确 定</el-button>
                <el-button @click="state = false"  size="small">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {enums} from '@/constant'
    import PlUpload from "../../../common/PlUpload"
    export default {
        components: {
            "pl-upload":PlUpload
        },
        data() {
            return {
                state: false,
                title: '',
                type:'',
                film:{
                    name: null,
                    url: null,
                    userId: null
                },
                rules: {
                    name: [
                        { required: true, message: '请输入底片名称', trigger: 'blur'}
                    ],
                    url: [
                        { required: true, message: '请上传底片', trigger: 'blur'}
                    ],
                    userId: [
                        { required: true, message: '请选择用户', trigger: 'blur'}
                    ]
                },
                options:[],
                attachment:[],//上传附件
            }
        },
        created() {},
        mounted() {
            // 获取用户信息
            this.axios({
                method: 'get',
                url: api.userManagement.userList
            }).then((response) => {
                if (response.data.code == 200) {
                    this.options = response.data.data.dataList;
                } else {
                    vm.$message.error(response.data.message);
                }
            })
        },
        methods: {
            showDialog(val){
                if(this.$refs['film']!==undefined){
                    this.$refs['film'].resetFields();
                }

                if(val){
                    for(var key in val){
                       this.film[key] = val[key];
                    }
                    this.film.bucket = this.urlFormat(this.film.url).bucket;
                    this.film.object = this.urlFormat(this.film.url).objectKey;
                    this.film.fileType = this.urlFormat(this.film.url).fileType;

                    if ( $.inArray(this.film.fileType, ["jpg", "jpeg","image/jpeg", "gif", "png", "bmp"]) < 0 ) {
                        this.film["isImage"] = false;
                        this.film["imgsrc"] = "/static/js/plupload/suffix/otherfile.png";
                    }else{
                        this.film["isImage"] = true;
                        this.film["imgsrc"] = this.film.url;
                    }
                    
                    this.film.fileRealName = this.film.objectKey;
                    this.film.imgsrc = this.film.url;
                    this.attachment.push(this.film);
                    this.title = "编辑底片";
                    this.type = "edit";
                }else{
                    this.title = "添加底片";
                    this.type = "add";
                }

                this.state = true;
            },
            addfilm(){
                let vm = this;
                vm.$refs.film.validate((valid) => {
                    if (valid) {

                        let submission =  ''; //提交方式

                        //选择提交方式
                        if(vm.type == 'edit'){
                            submission = 'put';
                        }else{
                            submission = 'post';
                        }
                        // 提交
                        vm.axios({
                            method: submission,
                            url: api.filmsManagement.filmsList,
                            data: vm.film
                        }).then((response) => {
                            if (response.data.code == 200) {
                                vm.state = false;
                                vm.$parent.$refs.filmTable.$emit('vuetable:refresh');
                                vm.$message.success(response.data.message);
                            } else {
                                vm.$message.error(response.data.message);
                            }
                        })
                    }else{
                        return false;
                    }
                });
            },
            reloadFiles: function (files) {
                let vm = this;
                this.attachment = [];

                files.forEach((item,idnex)=>{
                    item.url = item.fileStoreName;
                    this.attachment.push(item)
                })
                this.film.url = this.attachment[0].url;
            },
            // 格式化 url 地址
            urlFormat(strUrl){
                // var strUrl = "https://jstn-pic.oss-cn-qingdao.aliyuncs.com/d16e8272-4ac7-de6a-1071-a79ded59d29a.jpg"
                var arrUrl = strUrl.split("//")[1].split("/");
                var objectKey = arrUrl[1];
                var bucket = arrUrl[0].split(".")[0];
                var fileType = arrUrl[1].split(".")[1];
                var resultObject ={
                    bucket : bucket,
                    objectKey : objectKey,
                    fileType : fileType
                }
                return resultObject
            }
        },

    };
</script>
<style scoped>
.dialog-footer{ text-align:center; }
.dialog-footer button{ margin: 0 10%; }
</style>